package {
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	import fs.loader.Preloader;
	import fs.tileMap.Character;
	import fs.tileMap.TileMap;
	
	/**
	 * ...
	 * @author flyingSnow
	 */
	public class Main extends Sprite {
		private var _mapData:RegularMapData;
		private var _tilemap:TileMap;
		private var _char:Mario;
		
		public function Main():void {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			_mapData = new RegularMapData(200, 18, 0xffffff);
			_mapData.viewPort = new Rectangle(0, 0, 20, 15);
			
			_tilemap = new TileMap(_mapData, new MyTileFactory());
			addChild(_tilemap);
			_tilemap.hitManager = new MyHitManager();
			_tilemap.globalForce.x = 0;
			_tilemap.globalForce.y = 0.2;
			

			
			_tilemap.prepareTiles();
			//_tilemap.render();
			
			Preloader.instance.loader("mario.swf", start);
		}
		
		private function onFnterFrame(e:Event):void {
			_tilemap.loop();
			
			//var newViewPort:Rectangle = _mapData.viewPort;
			//newViewPort.x = _char.x / 40;
			//newViewPort.y = _char.y / 40;
		}
		
		private function onKeyUp(e:KeyboardEvent):void {
			switch(e.keyCode) {
				case Keyboard.LEFT:
					_char.v.x = 0;
					_char.stand();
					break;
				case Keyboard.RIGHT:
					_char.v.x = 0;
					_char.stand();					
					break;
				case Keyboard.UP:
					//if(_char.v.y)
					//_char.v.y = 0;
					break;
				case Keyboard.DOWN:
					//_char.v.y = 0;
					break;
			}
		}
		
		private function onKeyDown(e:KeyboardEvent):void {
			switch(e.keyCode) {
				case Keyboard.LEFT:
					//if((_char.hitFlag&1)==0){
						_char.v.x = -2;
						_char.walk();
					//}
					break;
				case Keyboard.RIGHT:
					//if((_char.hitFlag&2)==0){
						_char.v.x = 2;
						_char.walk();
					//}
					break;
				case Keyboard.UP:
					if((_char.hitFlag&4)!=0){
						_char.v.y = -10;
						_char.stand();
					}
					break;
				case Keyboard.DOWN:
						//_char.v.y = 10;
					break;
				case Keyboard.ENTER:
					_tilemap.globalForce.y = 0.2;
					break;
				case Keyboard.SPACE:
					_tilemap.globalForce.y = 0;
					break;
			}
		}
		
		private function start():void {
			_char = new Mario();
			_char.x = _char.y = 50;
			addChild(_char);
			_tilemap.mainChar = _char;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			stage.addEventListener(Event.ENTER_FRAME, onFnterFrame);
		}
		
		//private function runUp(e:Event):void {
			//stage.removeEventListener(Event.ENTER_FRAME, runUp);
			//var r:Rectangle = _mapData.viewPort;
			//r.y -= 0.2;
			//_mapData.viewPort = r;
			//_tilemap.render();
		//}
		//
		//private function runDown(e:Event):void {
		//stage.removeEventListener(Event.ENTER_FRAME, runDown);
			//var r:Rectangle = _mapData.viewPort;
			//r.y += 0.2;
			//_mapData.viewPort = r;
			//_tilemap.render();
		//}
		//
		//private function runRight(e:Event):void {
		//stage.removeEventListener(Event.ENTER_FRAME, runRight);
			//var r:Rectangle = _mapData.viewPort;
			//r.x += 0.2;
			//_mapData.viewPort = r;
			//_tilemap.render();
		//}
		//
		//private function runLeft(e:Event):void {
		//stage.removeEventListener(Event.ENTER_FRAME, runLeft);
			//var r:Rectangle = _mapData.viewPort;
			//r.x -= 0.2;
			//_mapData.viewPort = r;
			//_tilemap.render();
		//}
		
	}
	
}